package net.tv90.appserveradmin.aspect;

import cn.dev33.satoken.exception.NotLoginException;
import cn.dev33.satoken.exception.NotPermissionException;
import cn.dev33.satoken.exception.NotRoleException;
import net.tv90.appserveradmin.exception.ServerException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

@ControllerAdvice(annotations = Controller.class)
public class GlobalExceptionHandler {

    @ExceptionHandler(Exception.class)
    public String handleException(Exception ex) {
        return "error/other";
    }

    @ExceptionHandler(ServerException.class)
    public String handleServerException(ServerException ex) {
        switch (ex.getStatus()) {
            case UNAUTHORIZED -> {
                return "error/403";
            }
            case INTERNAL_SERVER_ERROR -> {
                return "error/500";
            }
            case NOT_FOUND -> {
                return "error/404";
            }
            default -> {
                return "error/other";
            }
        }
    }

    @ExceptionHandler(NotLoginException.class)
    public String handleNotLoginException(NotLoginException e) {
        return "error/403";
    }

    @ExceptionHandler(NotPermissionException.class)
    public String handleNotPermissionException(NotPermissionException e) {
        return "error/403";
    }

    @ExceptionHandler(NotRoleException.class)
    public String handleNotRoleException(NotRoleException e) {
        return "error/403";
    }

}
